#include "ControlScheme.h"
#include <windows.h>

XboxControllerScheme_1::XboxControllerScheme_1(int player)
{
	this->player = new CXBOXController(player);
	this->Exit = false;
	camera = NULL;
}

XboxControllerScheme_1::~XboxControllerScheme_1()
{
	delete player;
}

bool XboxControllerScheme_1::SetCharacter(YarnBall *character)
{
	if ( character != NULL )
	{
		this->character = character;
		return 1;
	}

	return 0;
}

bool XboxControllerScheme_1::SetCamera(Camera *camera)
{
	if( camera != NULL )
	{
		this->camera = camera;
		return 1;
	}

	return 0;
}

bool XboxControllerScheme_1::StartConrolling()
{
	if( character == NULL )
		return 0;
	else
	{
		if( player->IsConnected() )
				{
					//Right-Trigger Reserved for camera movement
					if( !player->GetState().Gamepad.bRightTrigger )
					{
						//Setting the left stick sensitivity in the positive direction of the x axis.

						if( (player->GetState().Gamepad.sThumbLX > 5000) && (player->GetState().Gamepad.sThumbLX < 15000))
						{
							character->movement(30.0f, 0.0f);
							
						}

						if( (player->GetState().Gamepad.sThumbLX > 15000) && (player->GetState().Gamepad.sThumbLX < 20000))
						{
							character->movement(60.0f, 0.0f);
						}

						if( (player->GetState().Gamepad.sThumbLX > 20000) && (player->GetState().Gamepad.sThumbLX < 25000))
						{
							character->movement(80.0f, 0.0f);
						}

						if( (player->GetState().Gamepad.sThumbLX > 25000) && (player->GetState().Gamepad.sThumbLX < 30000))
						{
							character->movement(100.0f, 0.0f);
						}

						if( (player->GetState().Gamepad.sThumbLX >= 30000) && (player->GetState().Gamepad.sThumbLX <= 32768))
						{
							character->movement(120.0f, 0.0f);
						}

						//Setting the left stick sensitivity in the negative direction of the x axis.

						if( (player->GetState().Gamepad.sThumbLX < -5000) && (player->GetState().Gamepad.sThumbLX > -15000))
						{
							character->movement(-30.0f, 0.0f);
						}

						if( (player->GetState().Gamepad.sThumbLX < -15000) && (player->GetState().Gamepad.sThumbLX > -20000))
						{
							character->movement(-60.0f, 0.0f);
						}

						if( (player->GetState().Gamepad.sThumbLX < -20000) && (player->GetState().Gamepad.sThumbLX > -25000))
						{
							character->movement(-80.0f, 0.0f);
						}

						if( (player->GetState().Gamepad.sThumbLX < -25000) && (player->GetState().Gamepad.sThumbLX > -30000))
						{
							character->movement(-100.0f, 0.0f);
						}

						if( (player->GetState().Gamepad.sThumbLX <= -30000) && (player->GetState().Gamepad.sThumbLX >= -32768))
						{
							character->movement(-120.0f, 0.0f);
						}

						//Setting the left stick sensitivity in the positive direction of the y axis

						if( (player->GetState().Gamepad.sThumbLY > 5000) && (player->GetState().Gamepad.sThumbLY < 15000))
						{
							//Nothing
						}

						if( (player->GetState().Gamepad.sThumbLY > 15000) && (player->GetState().Gamepad.sThumbLY < 20000))
						{
							//Nothing
						}

						if( (player->GetState().Gamepad.sThumbLY > 20000) && (player->GetState().Gamepad.sThumbLY < 25000))
						{
							//Nothing
						}

						if( (player->GetState().Gamepad.sThumbLY > 25000) && (player->GetState().Gamepad.sThumbLY < 30000))
						{
							//Nothing
						}

						if( (player->GetState().Gamepad.sThumbLY >= 30000) && (player->GetState().Gamepad.sThumbLY <= 32768))
						{
							//Nothing
						}

						//Setting the left stick sensitivity in the negative direction of the y axis

						if( (player->GetState().Gamepad.sThumbLY < -5000) && (player->GetState().Gamepad.sThumbLY > -15000))
						{
							//Nothing
						}

						if( (player->GetState().Gamepad.sThumbLY < -15000) && (player->GetState().Gamepad.sThumbLY > -20000))
						{
							//Nothing
						}

						if( (player->GetState().Gamepad.sThumbLY < -20000) && (player->GetState().Gamepad.sThumbLY > -25000))
						{
							//Nothing
						}

						if( (player->GetState().Gamepad.sThumbLY < -25000) && (player->GetState().Gamepad.sThumbLY > -30000))
						{
							//Nothing
						}

						if( (player->GetState().Gamepad.sThumbLY <= -30000) && (player->GetState().Gamepad.sThumbLY >= -32768))
						{
							//Nothing
						}


						///////////////
						///////////////
						///////////////

						if( (player->GetState().Gamepad.sThumbRX > 5000) && (player->GetState().Gamepad.sThumbRX < 10000))
						{
							//Nothing
							
						}

						if( (player->GetState().Gamepad.sThumbRX > 10000) && (player->GetState().Gamepad.sThumbRX < 32768))
						{
							//Nothing
						}

						if( (player->GetState().Gamepad.sThumbRX < -5000) && (player->GetState().Gamepad.sThumbRX > -10000))
						{
							//Nothing
						}

						if( (player->GetState().Gamepad.sThumbRX <= -10000) && (player->GetState().Gamepad.sThumbRX >= -32768))
						{
							//Nothing
						}

					}


					if(player->GetState().Gamepad.wButtons & XINPUT_GAMEPAD_A)
					{
						character->jump(100.0f);
						_cprintf("A Button Pressed!\n");
					}

					if(player->GetState().Gamepad.wButtons & XINPUT_GAMEPAD_Y)
					{
						//Nothing
						_cprintf("Y Button Pressed!\n");
					}

					if(player->GetState().Gamepad.wButtons & XINPUT_GAMEPAD_X)
					{
						//Nothing
					}

					if(player->GetState().Gamepad.wButtons & XINPUT_GAMEPAD_B)
					{
						this->Exit = true;
					}

					if( player->GetState().Gamepad.bRightTrigger)
					{
						//_cprintf("Test Right-Trigger\n");
						if(player->GetState().Gamepad.sThumbLX > 5000)
						{
							//Nothing
							_cprintf("Test Right-Trigger t1\n");
						}
						else if(player->GetState().Gamepad.sThumbLX < -5000)
						{
							//Nothing
							_cprintf("Test Right-Trigger t2\n");
						}

						if(player->GetState().Gamepad.sThumbLY > 5000)
						{
							//Nothing
							_cprintf("Test Right-Trigger t3\n");
						}
						else if(player->GetState().Gamepad.sThumbLY < -5000)
						{
							//Nothing
							_cprintf("Test Right-Trigger t4\n");
						}
						
					}
					
				}
	}

	return 1;
}

bool XboxControllerScheme_1::IsExiting()
{
	return Exit;
}


/////////////////////////////////////////////////////
//												   //
//		    KEYBOARD CONTROLLING SCHEME            //
//												   //
/////////////////////////////////////////////////////

KeyboardScheme_1::KeyboardScheme_1(int player)
{
	Exit = false;
}

KeyboardScheme_1::~KeyboardScheme_1()
{
}

bool KeyboardScheme_1::SetCharacter(YarnBall *character)
{
	if( character != NULL )
	{
		this->character = character;
		return 1;
	}

	return 0;
}

bool KeyboardScheme_1::SetCamera(Camera *camera)
{
	if( camera != NULL )
	{
		this->camera = camera;
		return 1;
	}

	return 0;
}

bool KeyboardScheme_1::StartConrolling(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
	//static bool c_obj = false;

    switch(message)
    {
        case WM_DESTROY:
            {
                PostQuitMessage(0);
                return 0;
            } break;

		case WM_CHAR:

			if('C' == wParam || 'c' == wParam)
			{
				//Nothing
			}

			if('A' == wParam || 'a' == wParam)
			{
				//Nothing
			}

			if('Z' == wParam || 'z' == wParam)
			{
				//Nothing
			}

			if('R' == wParam || 'r' == wParam)
			{
				//Nothing
			}

			if('L' == wParam || 'l' == wParam)
			{
				//Nothing
			}

			if('K' == wParam || 'k' == wParam)
			{
				//Nothing;
			}

			if('H' == wParam || 'h' == wParam)
			{
				_cprintf("HELP\n\nXBOX 360 Controller:\n\n"
						 "LeftThumb Stick -> moves yarnball\n"
						 "RightThumb Stick -> nothing\n"
						 "Button Y -> nothing\n"
						 "Button A -> yarnball jump\n"
						 "Button B -> nothing\n"
						 "\nKeyboard\n\n"
						 "Left  Puts a negative x axis force on the square\n"
						 "Right Puts a positive x axis force on the square\n"
						 "Pressing 'Shift' Zooms in\n"
						 "Pressing 'Enter' Zooms out\n"
						 "\n\nNote:If the square hits the edge of the world (yes that outline indicates the world)\n"
						 "then the square stops. You have to restart the app because I haven't figured out how to stop that yet.");
			}

			if('P' == wParam || 'p' == wParam)
			{
				//Nothing
			}


		break;

		case WM_KEYDOWN:

			if(VK_ESCAPE == wParam)
			{
				PostQuitMessage(0);
				Exit = true;
			}

			if(VK_UP == wParam)
			{
				//Nothing
			}
			else if(VK_DOWN == wParam)
			{
				//Nothing
			}

			if(VK_LEFT == wParam)
			{
				character->addForce(-1000, 0);
				character->movement(-100000*20);
			}
			else if(VK_RIGHT == wParam)
			{
				character->addForce(1000, 0);
				character->movement(100000*20);
			}

			if (VK_SPACE == wParam)
			{ 
				character->jump(10000*30);
			}

			if(VK_ADD == wParam)
			{
				//Nothing
				//_cprintf("mz:%f\n", mz);
			}
			else if(VK_SUBTRACT == wParam)
			{
				//Nothing
				//_cprintf("mz:%f\n", mz);
			}		

			break;

		case WM_KEYUP:
			break;
    }

	return 1;
}

bool KeyboardScheme_1::IsExiting()
{
	return Exit;
}